<template>
  <div>
    <div class="itemList">
      <div class="item" v-for="item of letters" :key="item" :ref="item" @click="handleLetterClick" @touchstart.prevent="handleTouchStart" @touchmove="handleTouchMove" @touchend="handleTouchEnd">{{item}}</div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'CityAlphabet',
  props: {
    cities: Object
  },
  data () {
    return {
      touchStatus: false,
      startY: 0,
      index: 0
    }
  },
  computed: {
    letters () {
      let letters = []
      for (let i in this.cities) {
        letters.push(i)
      }
      return letters
    }
  },
  updated () {
    this.startY = this.$refs['A'][0].offsetTop
  },
  methods: {
    handleLetterClick (e) {
      this.$emit('change', event.target.innerText)
    },
    handleTouchStart (e) {
      this.touchStatus = true
    },
    handleTouchMove (e) {
      if (this.touchStatus) {
        let touchY = e.touches[0].clientY - 79
        this.index = Math.floor((touchY - this.startY) / 20)
      }
    },
    handleTouchEnd () {
      this.touchStatus = false
      if (this.index >= 0 && this.index < this.letters.length) {
        this.$emit('change', this.letters[this.index])
      }
    }
  }
}
</script>

<style lang="stylus" scoped>
  @import '~styles/varibles.styl'
  .itemList
    display:flex
    flex-direction:column
    justify-content: center;
    position:absolute
    top:1.58rem
    right:0
    bottom:0
    width:.4rem
    z-index:100
    .item
      height:.4rem
      text-align:center
      color:$bgColor
</style>
